/* HARDWARE: Arduino
             AVR (Atmega 328)
             IC L293D
             DC motor
             IR sensor
  
  DESCRIPTION:Interrupt driven menu will be displayed as soon as you press the button
              you can select any functions from the menu.
              choice will be send through UART.//
                 
            
             
#include <EEPROM.h>                 
#define mpin1 6                     //motor pin
#define mpin2 7                     //motor pin
#define intrpt_pin 9                // pin number for calling menu
#define ct_of 800                   //sensor value for black 

char y;
int x,ar[]={0,0,0,0,0,0,0,0};
int i,r,j,k,m,playr;
int time,tm1,tm2,cntr,flag,sense;
int new_wnnr,prvs_wnnr,run; 
char ch;

void setup()
{ 
    attachInterrupt(0,menu,CHANGE);   //Interrput for calling menu
    pinMode(intrpt_pin,OUTPUT);
    pinMode(mpin1,OUTPUT);
    pinMode(mpin2,OUTPUT);
    Serial.begin(9600);
    initialise();
}
 
void loop()
{ 
    menu();


}
   
      void menu(void)               //function for displaying menu
        {
           Serial.print("enter choice");
           Serial.println("1. about");
           Serial.println("2. play");
           Serial.println("3. Histogram");
           Serial.println("4. profit and loss");
           Serial.println("5. exit");
           delay(10000); 
          if(Serial.available()>0)
                {
                   ch=Serial.read();
                   switch (ch)
                       {
  
                         case '2':
                          mtr_drv();
                          break;                                                                                                                                                                                                                                                                                                                                                                                                
  
                         case '3':
                          histogram();
                          break;
                         case '4':
                          prft_loss();
                          break;
                        case '5':
                          exit();
                          break;
                        case '1':
                          about();
                          break;  
                     }
                }
          }  
   void histogram(void)       //function for displaying histogram
   {
    for(i=1;i<512;i++)
      {
         r=EEPROM.read(i);
         ar[r-1]++;
      }
    for(j=0;j<8;j++)
      {
         Serial.print(j+1);
         for(k=0;k<ar[j];k++)
         Serial.print("*");
         Serial.println();
         delay(1000);
         ch=0;

       }
    for(i=0;i<8;i++)
       ar[i]=0;
  }

void mtr_drv(void)               //function for playing game
{
   tm1=millis();
   time=random(3000,10000);
   digitalWrite(mpin1,HIGH);
   digitalWrite(mpin2,LOW)  ; 
     while(tm1+time>millis())
      { 
 
         sense=analogRead(A0);
        
         flag= set_flag(sense);
        
           if(flag==1&&sense<ct_of)
            {
              cntr++;
              Serial.println(cntr);
            }  
      }
   digitalWrite(mpin1,LOW);  
   winner();
   ch=0; 
}
int set_flag(int snse)
{
  if(snse>ct_of)
  return 1;
  else
  return 0;
}
void initialise(void)    // function for intializing EEPROM
{ for(i=2;i<512;i++)
  EEPROM.write(i,-1);
  EEPROM.write(0,1);
  EEPROM.write(1,1);
}
void winner(void)       //function for calculating winner
{
  
   run=EEPROM.read(0);
   prvs_wnnr=EEPROM.read(run);
   cntr=cntr%8;
   new_wnnr=(cntr+prvs_wnnr)%8;
   EEPROM.write(0,run+1);
   EEPROM.write(run+1,new_wnnr);
}
void prft_loss(void)     //function for calculating profit and loss to all players
{
  int prft=100,loss=100, temp_wnnr,temp_looser;
  run=EEPROM.read(0);
  new_wnnr=EEPROM.read(run);
  temp_wnnr=new_wnnr;
  for(i=0;i<4;i++)
   {
      Serial.println();
      Serial.print(prft);
      if(temp_wnnr>8)
         temp_wnnr=temp_wnnr%8;
      Serial.print("% profit to player");
      Serial.print(temp_wnnr);
      temp_wnnr++;
      prft=prft-25;
   }                                                                                                                          
 
  temp_looser=temp_wnnr;
  for(j=0;j<4;j++)
  {
    Serial.println();
    Serial.print(loss);
    Serial.print("%loss to player");
    if(temp_looser>8)
      temp_looser=temp_looser%8;
    Serial.print(temp_looser);
    loss=loss-25;
    temp_looser++;
  }                             
ch=0;
}
void exit(void)        // function for exit
{
   noInterrupts();
   
}   
void about()
{
  Serial.println("Welcome to CASINO");
  Serial.println("There are 8 players");
  Serial.println("One player will get 100% profit and rest will get some profit or loss ");
}

